package eatwatch;

import java.util.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class FormCalories extends List implements CommandListener
{
	private final 	Command 		cmdBack  	= new Command ("Inapoi", Command.BACK, 0);
	private final 	Command 		cmdAdd  	= new Command ("Adauga", Command.SCREEN, 0);
	private	final	IDatabase		mDatabase   = EatWatch.mDatabase;
	private 		MIDlet			mMIDlet		= null;
	private 		Displayable		mPrevScreen	= null;
	private 		Hashtable 		mIds 		= null;

	// entry form stuff
	private 		Form			mEntryForm  	= null;
	private 		TextField 		txtIngredient	= null;
	private 		TextField 		txtCalories		= null;
	private 		ChoiceGroup		chgMeasure		= null;

	public FormCalories (MIDlet midlet)
	{
		super ("Calories", List.IMPLICIT);
		mMIDlet = midlet;
        addCommand (cmdAdd);
        addCommand (cmdBack);
        setCommandListener (this);
	}

	public void Show (Displayable prevForm)
	{
		mPrevScreen = prevForm;
		FillList ();
		Display.getDisplay (mMIDlet).setCurrent (this);
	}

	public void commandAction(Command c, Displayable d)
	{
		if (c == cmdBack) {
			if (d == this)
				Display.getDisplay (mMIDlet).setCurrent (mPrevScreen);
			else
				Display.getDisplay (mMIDlet).setCurrent (this);
		}
		else if (c == cmdAdd) {
			if (d == this)
				ShowEntryForm ();
			else {
				SaveEntry ();
				FillList ();
				Display.getDisplay (mMIDlet).setCurrent (this);
			}
		}
	}

	private void ShowEntryForm ()
	{
		if (mEntryForm == null)
			InitEntryForm ();
		Display.getDisplay (mMIDlet).setCurrent (mEntryForm);
	}

	private void InitEntryForm ()
	{
		mEntryForm 		= new Form ("Adauga ingredient");
		txtIngredient	= new TextField ("Ingredient", "", 50, TextField.ANY);
		txtCalories		= new TextField ("Calorii", "", 4, TextField.NUMERIC);
		chgMeasure		= new ChoiceGroup ("Masura", ChoiceGroup.POPUP, new String[] {"100g", "buc"}, null);

		mEntryForm.append (txtIngredient);
		mEntryForm.append (txtCalories);
		mEntryForm.append (chgMeasure);

		mEntryForm.addCommand (cmdBack);
		mEntryForm.addCommand (cmdAdd);
		mEntryForm.setCommandListener (this);
	}

	private void FillList ()
	{
		deleteAll ();
		mIds = new Hashtable ();

		mDatabase.Open ("calories", "ingredient");
		Record rec = null;
		while ((rec = mDatabase.ReadRecord ()) !=null) {
			StringBuffer ingredient = new StringBuffer (rec.GetField ("ingredient").toString ());
			ingredient.append (" = ");
			ingredient.append (rec.GetField ("calories").toString ());
			ingredient.append (" / ");
			ingredient.append ((((Integer)rec.GetField ("MU")).intValue () == 0 ? "100g" : "buc"));
			int idx = append (ingredient.toString (), null);
			mIds.put (new Integer (idx), new Integer (rec.GetId ()));
		}
		mDatabase.Close ();
	}

	private void SaveEntry ()
	{
		Record rec = new Record ();
		rec.SetField ("ingredient", txtIngredient.getString ());
		rec.SetField ("calories", new Integer (Integer.parseInt (txtCalories.getString ())));;
		rec.SetField ("MU", new Integer (chgMeasure.getSelectedIndex ()));;
		mDatabase.Open ("calories");
		mDatabase.AddRecord (rec);
		mDatabase.Close ();
	}
}